package com.yanqu.road.server.logic.bag;

import com.yanqu.road.entity.bag.UseGoodsResult;
import com.yanqu.road.entity.config.goods.GoodsInfo;
import com.yanqu.road.entity.enums.eGoodsType;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.entity.log.LogUseGoodsChangeEarn;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.manger.activity.cookboy.CookBoyActivityMgr;
import com.yanqu.road.server.manger.activity.deepfishing.DeepFishingMgr;
import com.yanqu.road.server.manger.activity.fivehero.FiveHeroMgr;
import com.yanqu.road.server.manger.activity.foxcard.FoxCardMgr;
import com.yanqu.road.server.manger.activity.pawnshop.PawnshopMgr;
import com.yanqu.road.server.manger.activity.springdinner.SpringDinnerMgr;
import com.yanqu.road.server.manger.activity.suitang.SuiTangMgr;
import com.yanqu.road.server.manger.activity.twins.TwinsMgr;
import com.yanqu.road.server.manger.activity.zhenhuan.ZhenHuanConfigMgr;
import com.yanqu.road.server.manger.log.AutoLogMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.utils.property.Property;

import java.math.BigInteger;
import java.util.Objects;

public abstract class BaseUseGoods {

	protected GamePlayer player;
	protected GoodsInfo goodsInfo;
	protected long param;
	protected String param2;

	public BaseUseGoods(GamePlayer player, GoodsInfo goodsInfo, long param) {
		this.player = player;
		this.goodsInfo = goodsInfo;
		this.param = param;
	}

	public BaseUseGoods(GamePlayer player, GoodsInfo goodsInfo, long param, String param2) {
		this.player = player;
		this.goodsInfo = goodsInfo;
		this.param = param;
		this.param2 = param2;
	}

	public UseGoodsResult useGoods(long count){
		UseGoodsResult useResult = new UseGoodsResult();
		int ret = checkUse(count);
		if(0 != ret){
			useResult.setRet(ret);
			return useResult;
		}
		if(!player.getModule(CurrencyModule.class).removeCurrency(new Property(goodsInfo.getGoodsId(), BigInteger.valueOf(count)),
				eLogMoneyType.Bag, eLogMoneyType.BagUse)){
			useResult.setRet(GameErrorCode.E_BAG_ITEM_NO_ENOUGH);
			return useResult;
		}
		String beforeEarn = player.getUserInfo().getEarnSpeed().toString();
		UseGoodsResult result = executeUse(count);
		if(result.getRet() == 0){
			String afterEarn = player.getUserInfo().getEarnSpeed().toString();
			if(!Objects.equals(beforeEarn, afterEarn)){
				AutoLogMgr.add(new LogUseGoodsChangeEarn(player.getUserId(), goodsInfo.getGoodsId(), count, beforeEarn, afterEarn));
			}
		}
		return result;
	}

	abstract int checkUse(long count);

	abstract UseGoodsResult executeUse(long count);

	public static BaseUseGoods getUseGoodsEntity(GamePlayer player, GoodsInfo goodsInfo, long param, String param2) {
		if(goodsInfo.getType() == eGoodsType.AddSilver.getValue()){
			return new UseAddSilver(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.AddPatronsExp.getValue()) {
			return new UseAddPatronsExp(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.BeautyEnergyRecover.getValue()) {
			return new UseRecoverBeautyEnergy(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.AddPrestige.getValue()) {
			return new UseAddPrestige(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.AddStaff.getValue()) {
			return new UseAddStaff(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.AddPatronsAbility.getValue()) {
			return new UseAddPatronsAbility(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.AddTargetPatronsAbility.getValue()) {
			return new UseAddTargetPatronsAbility(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.AddTargetPatronsQualification.getValue()) {
			return new UseAddTargetPatronsQualification(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.AddBeautyIntimacy.getValue()){
			return new UseAddBeautyIntimacy(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.AddBeautyGlamour.getValue()) {
			return new UseAddBeautyGlamour(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.GoodsBag.getValue() || goodsInfo.getType() == eGoodsType.SpecialBox.getValue()) {
			if(FiveHeroMgr.getConfig() != null){
				if(goodsInfo.getGoodsId() == FiveHeroMgr.getConfig().getBookItemId()){
					return new UseFiveHeroBookGoodsBag(player, goodsInfo, param);
				}
			}
			return new UseGoodsBag(player, goodsInfo, param);
		} else if (goodsInfo.getType() == eGoodsType.SpecialPatronsSkinBox.getValue()) {
			return new UseSpecialPatronsSkinGoodsBag(player, goodsInfo, param);
		} else if (goodsInfo.getType() == eGoodsType.WaSheSkillBox.getValue()){
			return new UseWasheSkillBag(player, goodsInfo, param);
		} else if(goodsInfo.getType() == eGoodsType.TravelPowerRecover.getValue()) {
			return new UseRecoverTravelPower(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.Skin.getValue()) {
			return new UseSkin(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.OreWarHp.getValue()) {
			return new UseRecoverOreWarHpPower(player, goodsInfo, param);//param传的是活动id，对应活动id使用体力药
		}else if(goodsInfo.getType() == eGoodsType.PlatformGoods.getValue()) {
			return new UsePlatformGoods(player, goodsInfo, param);//param传的是活动id，对应活动id使用体力药
		}else if (goodsInfo.getType() == eGoodsType.OptionalBox.getValue() || goodsInfo.getType() == eGoodsType.TravelBookOptionalPack.getValue()) {
			return new UseOptionalBox(player, goodsInfo, param, param2); // param2传选择的奖励
		}else if (goodsInfo.getType() == eGoodsType.OptionalBoxEx.getValue()) {
			return new UseOptionalBox(player, goodsInfo, param, param2); // param2传选择的奖励
		}else if (goodsInfo.getType() == eGoodsType.AllGoodsBag.getValue()) {
			return new UseGoodsBagAllGet(player, goodsInfo, param);
		}else if (goodsInfo.getType() == eGoodsType.AddBaiYeExp.getValue()) {
			return new UseAddPatronsBaiYeExp(player, goodsInfo, param);
		} else if (goodsInfo.getType() == eGoodsType.AddManorOutPut.getValue()) {
			return new UseAddManorOutPut(player, goodsInfo, param);
		} else if (goodsInfo.getType() == eGoodsType.BeautyEscortAccelerate.getValue()) {
			return new UseReduceBeautyEscortTime(player, goodsInfo, param);
		} else if(goodsInfo.getType() == eGoodsType.RecoverMonopolyMarketPhysical.getValue()) {
			return new UseRecoverMonopolyMarketPhysical(player, goodsInfo, param);
		} else if(goodsInfo.getType() == eGoodsType.RecoverNewMonopolyMarketPhysical.getValue()) {
			return new UseRecoverNewMonopolyMarketPhysical(player, goodsInfo, param);
		} else if(goodsInfo.getType() == eGoodsType.RandomAddBeautyExp.getValue()) {
			return new UseRandomAddBeautyExp(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.TongBingRedPacketKey.getValue()) {
			return new UseAddTongBingRedPacket(player, goodsInfo, param);
		} else if(goodsInfo.getType() == eGoodsType.SuiTangRedPacketKey.getValue()) {
			return new UseAddSuiTangRedPacket(player, goodsInfo, param);
		} else if(goodsInfo.getType() == eGoodsType.TonBingRedPacketKey.getValue()) {
			return new UseAddTonBingRedPacket(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.ChuHanRedPacket.getValue()) {
			return new UseAddChuHanRedPacket(player, goodsInfo, param);
		} else if(goodsInfo.getType() == eGoodsType.VehicleSoulItem.getValue()) {
			return new UseAddVehicleSoul(player, goodsInfo, param);
		}else if(goodsInfo.getType() == eGoodsType.MusicRoomGetTimerReward.getValue()){
			return new UseAddMusicRoomTimerReward(player, goodsInfo, param);
		} else if (goodsInfo.getType() == eGoodsType.TravelBookRandomPack.getValue()) {
			return new UserTravelBookRandomPack(player, goodsInfo, param);
		} else {
			if(CookBoyActivityMgr.getCookBoyHpItemId((int)param) == goodsInfo.getGoodsId()) {
				return new UseRecoverCookBoy1HpPower(player, goodsInfo, param);//param传的是活动id，对应活动id使用体力药
			}
			if(SpringDinnerMgr.getRecoverItemId() == goodsInfo.getGoodsId()) {
				return new UseRecoverSpringDinnerGameHpPower(player, goodsInfo, param);
			}
			if (PawnshopMgr.config != null) {
				if (PawnshopMgr.config.staminaGoodsId == goodsInfo.getGoodsId()) {
					return new UseRecoverPawnshopPower(player, goodsInfo, param);
				}
			}
			if (DeepFishingMgr.getConfig() != null){
				if (DeepFishingMgr.getConfig().getFishingPowerItemId() == goodsInfo.getGoodsId() || DeepFishingMgr.getConfig().getBeachCombPowerItemId()== goodsInfo.getGoodsId()){
					return new UseRecoverDeepFishingPower(player, goodsInfo, param);
				}
			}
			if(TwinsMgr.getConfig() != null){
				if(goodsInfo.getGoodsId() == TwinsMgr.getConfig().getPveHpItemId()){
					return new UseRecoverTwinsPveHpPower(player, goodsInfo, param);
				}
				if (goodsInfo.getGoodsId() == TwinsMgr.getConfig().getPvpHpItemId()) {
					return new UseRecoverTwinsPvpHpPower(player, goodsInfo, param);
				}
			}
			if (FiveHeroMgr.getConfig() != null) {
				if (goodsInfo.getGoodsId() == FiveHeroMgr.getConfig().getEnergItemId()) {
					return new UseRecoverFiveHeroHpPower(player, goodsInfo, param);
				}
			}

			if (ZhenHuanConfigMgr.isZhenHuanPowerGoods(goodsInfo.getGoodsId())) {
				return new UseRecoverZhenHuanPower(player, goodsInfo, param);
			}
			if (SuiTangMgr.getConfig() != null) {
				if (goodsInfo.getGoodsId() == SuiTangMgr.getConfig().getParamConfig().getMuBingLingItem()) {
					return new UseSuiTangMuBingLing(player, goodsInfo, param);
				}
			}
			if (FoxCardMgr.getConfig() != null) {
				if (goodsInfo.getGoodsId() == FoxCardMgr.getConfig().getLotteryPowerId()) {
					return new UseRecoverFoxCardPower(player, goodsInfo, param);
				}
			}
			return null;
		}
	}
}
